package com.cxg.fairy.ec.main.search;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.EcBottomDelegate;
import com.cxg.fairy.ec.main.index.search.SearchDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.util.MyDividerItemDecoration;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by CFairy
 */

public class SearchListDelegate extends LatteDelegate implements ISuccess {

    private SearchListAdapter mAdapter = null;

    private static final String ARG_SEARCH_NAME = "ARG_SEARCH_NAME";
    private String searchTag = "";

    @BindView(R2.id.rv_list_content)
    RecyclerView mRecyclerView = null;

    @BindView(R2.id.m_main_fab)
    FloatingActionButton mShopCartButton = null;

    @BindView(R2.id.m_main_back)
    FloatingActionButton mSearchListBackButton = null;

    @BindView(R2.id.tv_search_tag_name)
    AppCompatTextView tvSearchTagName = null;

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @OnClick(R2.id.m_main_back)
    void doActionButtonBack() {
        getSupportDelegate().pop();
    }

    @OnClick(R2.id.m_main_fab)
    void toShopCartDelegate() {
        getSupportDelegate().start(new EcBottomDelegate(3));
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TabLayout searchListTb = (TabLayout) view.findViewById(R.id.search_list_tab_layout);

        searchListTb.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(final TabLayout.Tab tab) {
                // position =  0 , 1 , 2 , 3  对应
                RestClient.builder()
                        .url("getSPKUBySearchNameFuzzy")
                        .params("uuid", AccountManager.getSignUUID())
                        .params("searchName",searchTag)
                        .params("cityId",1)
                        .params("sortType",tab.getPosition()+1)
                        .loader(getContext())
                        .success(new ISuccess() {
                            @Override
                            public void onSuccess(String response) {
                                final ArrayList<MultipleItemEntity> data = new SearchListDataConverter().setJsonData(response).convert();
                                mAdapter = new SearchListAdapter(data);
                                mAdapter.notifyDataSetChanged();
                                mRecyclerView.setAdapter(mAdapter);
                            }
                        })
                        .build()
                        .post();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    @OnClick(R2.id.itv_search_btn)
    void toSearchDelegate() {
        getSupportDelegate().start(new SearchDelegate());
    }

    public static SearchListDelegate create(String searchTag) {
        final Bundle args = new Bundle();
        args.putString(ARG_SEARCH_NAME, searchTag);
        final SearchListDelegate delegate = new SearchListDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_list_goods;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        final Bundle args = getArguments();
        if (args != null) {
            searchTag = args.getString(ARG_SEARCH_NAME);
            tvSearchTagName.setText(searchTag);
        }

        mShopCartButton.setAlpha(0.7f);
        mSearchListBackButton.setAlpha(0.6f);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        RestClient.builder()
                .url("getSPKUBySearchNameFuzzy")
                .params("uuid",AccountManager.getSignUUID())
                .params("searchName",searchTag)
                .params("sortType",0)
                .params("cityId",1)
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }

    @SuppressLint("NewApi")
    @Override
    public void onSuccess(String response) {
        final ArrayList<MultipleItemEntity> data = new SearchListDataConverter().setJsonData(response).convert();
        mAdapter = new SearchListAdapter(data);
        final GridLayoutManager manager = new GridLayoutManager(getContext(), 30);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
        MyDividerItemDecoration itemDecoration = new MyDividerItemDecoration(this.getContext());
        mRecyclerView.addItemDecoration(itemDecoration);

        mRecyclerView.addOnItemTouchListener(SearchListItemClickListener.create(this));
    }


}
